package cn.icanci.loopstack.ddk.admin.biz.service.impl;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.ddk.admin.biz.event.log.LogEvent;
import cn.icanci.loopstack.ddk.admin.biz.mapping.config.GroupMapping;
import cn.icanci.loopstack.ddk.admin.biz.service.BaseService;
import cn.icanci.loopstack.ddk.admin.biz.service.GroupService;
import cn.icanci.loopstack.ddk.admin.dal.mapper.entity.GroupDO;
import cn.icanci.loopstack.ddk.admin.dal.mapper.mapper.GroupMapper;
import cn.icanci.loopstack.ddk.common.enums.LogOperatorTypeEnum;
import cn.icanci.loopstack.ddk.common.enums.ModuleTypeEnum;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.TextValue;
import cn.icanci.loopstack.ddk.common.model.config.GroupVO;
import cn.icanci.loopstack.ddk.common.utils.PaginatorUtils;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 21:06
 */
@Service
public class GroupServiceImpl extends BaseService<GroupVO> implements GroupService {

    @Resource
    private GroupMapper  groupMapper;
    @Resource
    private GroupMapping groupMapping;

    @Override
    public PageList<GroupVO> queryPage(GroupVO vo, int currentPage, int pageSize) {

        vo.setEnv(getEnv());
        vo.setIsDelete(null);

        currentPage = Math.max(currentPage - 1, 0);

        GroupDO ddkGroup = groupMapping.vo2do(vo);
        List<GroupDO> ret = groupMapper.selectPage(ddkGroup, currentPage, pageSize);

        return PaginatorUtils.formatPaginator(groupMapping.dos2vos(ret), currentPage, pageSize, groupMapper.total(ddkGroup));
    }

    @Override
    public void save(GroupVO vo) {
        if (doInsert(vo)) {
            beforeInsert(vo);
            GroupDO ddkGroup = groupMapping.vo2do(vo);
            groupMapper.insert(ddkGroup);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_GROUP, JSONUtil.toJsonStr(ddkGroup), LogOperatorTypeEnum.CREATE), false);
        } else {
            beforeUpdate(vo);
            GroupDO ddkGroup = groupMapping.vo2do(vo);
            groupMapper.update(ddkGroup);
            eventDispatcher.fire(new LogEvent(vo.getUuid(), ModuleTypeEnum.DDK_GROUP, JSONUtil.toJsonStr(ddkGroup), LogOperatorTypeEnum.UPDATE), false);
        }
    }

    @Override
    public GroupVO queryByGroupId(String groupId) {
        GroupDO group = groupMapper.selectByGroupId(groupId, getEnv());
        return groupMapping.do2vo(group);
    }

    @Override
    public GroupVO queryByGroupName(String groupName) {
        GroupDO group = groupMapper.selectByGroupName(groupName, getEnv());
        return groupMapping.do2vo(group);
    }

    @Override
    public List<TextValue> loadSelector() {
        GroupDO groupDO = new GroupDO();
        groupDO.setEnv(getEnv());

        List<GroupDO> groups = groupMapper.selectList(groupDO);

        if (CollectionUtils.isEmpty(groups)) {
            return Lists.newArrayList();
        }

        List<GroupVO> groupVOS = groupMapping.dos2vos(groups);
        List<TextValue> textValues = Lists.newArrayList();
        for (GroupVO group : groupVOS) {
            String label;
            if (isDeleted(group)) {
                label = String.format(DELETED_FORMAT, group.getGroupName());
            } else {
                label = String.format(NOT_DELETED_FORMAT, group.getGroupName());
            }
            String value = group.getUuid();
            textValues.add(new TextValue(label, value));
        }
        return textValues;
    }

    @Override
    public GroupVO queryByGroupUuid(String groupUuid) {
        GroupDO group = groupMapper.selectByGroupUuid(groupUuid, getEnv());
        return groupMapping.do2vo(group);
    }

}
